package dao.impI;

import bean.Fruit;
import dao.FruitDao;
import util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class FruitDaoImpI implements FruitDao {
    static Connection conn=null;
    static PreparedStatement pps=null;
    static ResultSet resultSet=null;
    static DBUtil dbUtil = new DBUtil();

    /**
     * 添加产品
     * @param fruit
     * @return
     */
    @Override
    public boolean addFruit(Fruit fruit) {
        conn = dbUtil.getConn();
        String sql="insert into fruit values(null,?,?,?,?)";
        try {
            pps = conn.prepareStatement(sql);
            pps.setString(1,fruit.getFname());
            pps.setDouble(2,fruit.getFprice());
            pps.setString(3, fruit.getFdate());
            pps.setDouble(4,fruit.getFweight());
            int rs = pps.executeUpdate();
            if (rs>0){
                return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            dbUtil.close();
        }
        return false;
    }

    /**
     * 获取所有产品信息
     * @return
     */
    @Override
    public List<Fruit> findAllFruit() {
        List fruitList=new ArrayList();
        conn = dbUtil.getConn();
        String sql="select * from fruit";
        try {
            pps = conn.prepareStatement(sql);
            resultSet = pps.executeQuery();
            while(resultSet.next()){
                Fruit fruit = new Fruit();
                fruit.setId(resultSet.getInt("id"));
                fruit.setFname(resultSet.getString("fname"));
                fruit.setFprice(resultSet.getDouble("fprice"));
                fruit.setDate(resultSet.getString("fdate"));
                fruit.setFweight(resultSet.getDouble("fweight"));
                System.out.println("当前产品:"+fruit);
                fruitList.add(fruit);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            dbUtil.close();
        }
        return fruitList;
    }

    /**
     * 通过id查询
     * @param id
     * @return
     */
    @Override
    public Fruit findProductById(int id) {
        Fruit fruit = new Fruit();
        conn = dbUtil.getConn();
        String sql="select * from fruit where id=?";
        try {
            pps = conn.prepareStatement(sql);
            pps.setInt(1,id);
            ResultSet resultSet = pps.executeQuery();
            while(resultSet.next()){
                fruit.setId(resultSet.getInt("id"));
                fruit.setFname(resultSet.getString("fname"));
                fruit.setFprice(resultSet.getDouble("fprice"));
                fruit.setDate(resultSet.getString("fdate"));
                fruit.setFweight(resultSet.getDouble("fweight"));
            }
            System.out.println("通过id查询到的结果是:"+fruit);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            dbUtil.close();
        }
        return fruit;
    }

    /**
     * 更新
     * @param fruit
     * @return
     */
    @Override
    public boolean updateProduct(Fruit fruit) {
        conn = dbUtil.getConn();
        String sql="update fruit set fname=?,fprice=?,fdate=?,fweight=? where id=?";
        try {
            pps = conn.prepareStatement(sql);
            pps.setString(1,fruit.getFname());
            pps.setDouble(2,fruit.getFprice());
            pps.setString(3, fruit.getFdate());
            pps.setDouble(4,fruit.getFweight());
            pps.setInt(5,fruit.getId());
            int rs = pps.executeUpdate();
            if (rs>0){
                return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            dbUtil.close();
        }
        return false;
    }

    /**
     * 通过id删除
     * @param id
     * @return
     */
    @Override
    public boolean deleteProductById(int id) {
        conn = dbUtil.getConn();
        String sql="delete from fruit where id=?";
        try {
            pps = conn.prepareStatement(sql);
            pps.setInt(1,id);
            int rs = pps.executeUpdate();
            System.out.println("删除状态:"+rs);
            if (rs>0){
                return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            dbUtil.close();
        }
        return false;
    }
}
